﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PASNew.UserControls
{
    public partial class BackendUC : System.Web.UI.UserControl
    {
        string makeCamel(string original)
        {
            if (string.IsNullOrEmpty(original)) return string.Empty;
            // convert to char array of the string    
            char[] letters = original.ToCharArray();
            // upper case the first char    
            letters[0] = char.ToUpper(letters[0]);
            // return the array made of the new char array    
            return new string(letters);
        } //used to change text to the correct format

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserIdentity"] != null)
            {
                if (IsPostBack == false)
                {
                    UserDetailsDto user = (UserDetailsDto)Session["UserIdentity"];

                    using (PASDataDataContext data = new PASDataDataContext())
                    {
                        var Alevel = (from a in data.UserDetails
                                      where a.UserId == user.UserID
                                      select a).FirstOrDefault();

                        if (Alevel != null)
                        {
                            if (Alevel.UserLevel != 1)
                            {
                                Response.Redirect("Home.aspx");
                            }
                            else
                            {
                                var loadCompanies = from c in data.CompanyDetails
                                                    select new ListItem
                                                    {
                                                        Text = c.CompanyName,
                                                        Value = c.CompanyId.ToString(),
                                                    };
                                foreach (ListItem l in loadCompanies)
                                {
                                    DDLoadCompanies.Items.Add(l);
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void DDLoadCompanies_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLoadNames.Items.Clear();
            DDLoadNames.Items.Add("Please select user ---");
            int numbers = 0;
            int compID = int.Parse(DDLoadCompanies.SelectedValue.ToString());

            using (PASDataDataContext data = new PASDataDataContext())
            {
                var loadRelationships = from c in data.CompanyUserRelations
                                        where c.CompanyId == compID
                                        select new ListItem
                                        {
                                            Value = c.UserId.ToString(),
                                        };
                foreach (ListItem ls in loadRelationships)
                {

                    var loadNames = from d in data.UserDetails
                                    where d.UserId == int.Parse(ls.Value)
                                    select new ListItem
                                    {
                                        Text = d.UserName,
                                        Value = d.UserId.ToString(),
                                    };


                    foreach (ListItem l in loadNames)
                    {
                        DDLoadNames.Items.Add(l);
                        numbers++;
                    }

                    lblNumber.Text = "Users: " + numbers.ToString();
                    lblNumber.Visible = true;
                }
            }
        }     

        protected void DDLoadNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (PASDataDataContext data = new PASDataDataContext())
            {
                var Details = (from u in data.UserDetails
                               where u.UserId.ToString() == DDLoadNames.SelectedValue
                               select u).FirstOrDefault();

                txtBoxUserName.Text = Details.UserName;
                txtBoxUserEmail.Text = Details.UserEmail;
                txtBoxPassword.Text = Details.UserPassword;
                txtBoxUserALevel.Text = Details.UserLevel.ToString();
            }
        }

        protected void btnNewCompany_Click(object sender, EventArgs e)
        {
            DDLoadCompanies.Visible = false;
            tbCompanyName.Visible = true;
        }

        protected void btnSaveCompany_Click(object sender, EventArgs e)
        {
            using (PASDataDataContext data = new PASDataDataContext())
            {
                CompanyDetail NewComp = new CompanyDetail();

                NewComp.CompanyName = makeCamel(tbCompanyName.Text.ToString());

                data.CompanyDetails.InsertOnSubmit(NewComp);
                data.SubmitChanges();


                DDLoadCompanies.Visible = true;
                tbCompanyName.Visible = false;
            }
            Response.Redirect("backend.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //still neeed to do. ZYou spoke of just disabbling

        }

        protected void btnNewUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("Backend.aspx");
        }

        protected void btnSaveUser_Click(object sender, EventArgs e)
        {
            using (PASDataDataContext data = new PASDataDataContext())
            {

                var check = (from c in data.UserDetails
                             where c.UserEmail == txtBoxUserEmail.Text.ToString()
                             select c).FirstOrDefault();

                if (check != null) //if true update user details
                {
                    check.UserName = makeCamel(txtBoxUserName.Text);
                    check.UserEmail = txtBoxUserEmail.Text.ToString();
                    check.UserPassword = txtBoxPassword.Text.ToString();
                    check.UserLevel = int.Parse(txtBoxUserALevel.Text.ToString());

                    lblSuccess.Text = "User successfully updated";
                    lblSuccess.Visible = true;

                    data.SubmitChanges();
                }
                else
                {
                    UserDetail Save = new UserDetail();

                    Save.UserName = makeCamel(txtBoxUserName.Text);
                    Save.UserEmail = txtBoxUserEmail.Text.ToString();
                    Save.UserPassword = txtBoxPassword.Text.ToString();
                    Save.UserLevel = int.Parse(txtBoxUserALevel.Text.ToString());

                    data.UserDetails.InsertOnSubmit(Save);
                    data.SubmitChanges();

                    var loadsave = (from c in data.UserDetails
                                    where c.UserEmail == txtBoxUserEmail.Text.ToString()
                                    select c).FirstOrDefault();

                    CompanyUserRelation NewRelationship = new CompanyUserRelation();

                    NewRelationship.UserId = int.Parse(loadsave.UserId.ToString());
                    NewRelationship.CompanyId = int.Parse(DDLoadCompanies.SelectedValue.ToString());

                    data.CompanyUserRelations.InsertOnSubmit(NewRelationship);
                    data.SubmitChanges();

                    lblSuccess.Text = "User successfully saved";
                    lblSuccess.Visible = true;
                }
            }
        }

        protected void btnDeleteUser_Click(object sender, EventArgs e)
        {
            int identity;

            using (PASDataDataContext data = new PASDataDataContext())
            {
                var x = (from c in data.UserDetails
                         where c.UserEmail == txtBoxUserEmail.Text.ToString()
                         select c).FirstOrDefault();

                identity = x.UserId;
                DDLoadNames.Items.Remove(x.UserName.ToString());

                data.UserDetails.DeleteOnSubmit(x);

                var z = (from y in data.CompanyUserRelations
                         where y.UserId == identity
                         select y).FirstOrDefault();

                data.CompanyUserRelations.DeleteOnSubmit(z);
                data.SubmitChanges();
            }

            lblSuccess.Text = "User successfully deleted";
            lblSuccess.Visible = true;
            Response.Redirect("Backend.aspx");
        }

        
       
    }
}